<?php
namespace backend\controllers;

use backend\models\AuthGroupAccess;
use common\components\BaseController;
use Yii;
use yii\db\Query;
use backend\models\Admin;
use \common\helps\tools;
use \common\helps\code;


/**
 * Admin controller
 */
class AdminController extends BaseController
{

    public $modelClass = 'backend\models\Admin';
    public function init()
    {
//        $this->enableAuth=false;
        parent::init();
    }

    /**
     * 获取后台用户列表
     * @param Request $request
     * @author cpy(1442435897@qq.com)
     * @return Json
     */
    public  function  actionGetList(){
        $request =$this->request;
        $model = new Admin();
        $params = $request->queryParams;
        $total_data = $model->selectData('`id`,`true_name`,`username`,`mobile`,`super`',$params);
        $data = tools::getPageData($total_data,empty($params['page_size'])?'':$params['page_size'],empty($params['page'])?'':$params['page']);

        if(!empty($params['excel_download']) && $params['excel_download']>0){
            $tableHeader = ['id','姓名','用户名','手机号'];
            foreach($total_data as &$value){
                unset($value['super']);
            }
            tools::ToPHPEXECL($tableHeader,$total_data,'后台管理员列表');
        }
        return code::success($data);
    }

    /**
     * 后台用户编辑信息
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public function actionAdminEdit(){
        $request =$this->request;
        $model = new Admin();
        if($request->isPost){
            $params = $request->bodyParams;
            if(empty($params['id']))   return code::error('','参数错误');
            $model->scenario = 'user_info';
            if($model->load($params,'')){
                if($model->validate()){
                    $model::updateUserInfo($this->request->bodyParams,$params['id']);
                    if(empty($model->getErrors())){
                        return code::success('','编辑成功!');
                    }else{
                        return code::$error;
                    }
                }else{
                    $error = array_values($model->getErrors());
                    return code::error('',$error[0][0]);
                }
            }
        }
    }

    /**
     * 获取后台用户的信息
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public function actionGetAdminInfo(){
        $model = new Admin();
        $request =$this->request;
        $params = $request->bodyParams;
        $user_info = $model->findOne(['id' => $params['id']]);
        unset($user_info['password']);
        unset($user_info['salt']);
        unset($user_info['token']);
        return code::success($user_info);
    }

    /**
     * 后台用户修改密码
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionEditPassword(){
        $model = new Admin();
        $model->scenario = 'modify';
        if($model->load($this->request->bodyParams,'')){
            if($model->validate()){
                $params=$this->request->bodyParams;
                 $model::modifyPassword($params,$params['id']);
                if(empty($model->getErrors())){
                    return code::success('','修改密码成功!');
                }else{
                    return code::$error;
                }
            }else{
                $error = array_values($model->getErrors());
                return code::error('',$error[0][0]);
            }
        }


    }

    /**
     * 新增后台登录用户
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public function actionAdminAdd(){
        $request =$this->request;
        $model = new Admin();
        if($request->isPost){
            $params = $request->bodyParams;
            if($model->load($params,'')){
                if($model->validate()){
                    $params['salt'] = tools::generateRandomString(4);
                    $params['password'] = sha1(md5($params['password'].$params['salt']));
                    $res = $model->saveUserInfo('create',$params);
                    if(empty($res->getErrors())){
                        return code::success('','新增用户成功!');
                    }else{
                        return code::$error;
                    }
                }else{
                    $error = array_values($model->getErrors());
                    return code::error('',$error[0][0]);
                }
            }
        }
    }

    /**
     * 删除用户
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionDelAdmin(){
        if($this->request->isPost) {
            $admin_model = new Admin();
            $params = $this->request->bodyParams;
            if(empty($params['id'])){
                return code::error('','参数错误');
            }
            $user_id= intval($params['id']);
            $session = Yii::$app->session;
            $current_user_id =  $session->get('user_id');
            if($current_user_id==$user_id) return code::error('','不允许删除当前登录账号');
            $res = $admin_model->deleteUser($user_id);
            if($res){
                return code::success();
            }else{
                return code::error('','操作失败');
            }
        }
    }

    /**
     * 批量删除
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionBatchDelAdmin(){
        if($this->request->isPost) {
            $request =$this->request;
            $model = new Admin();
            $params = $request->bodyParams;
            if(empty($params['ids'])) return code::error('error','参数错误');
            $data=$model->batchDel($params);
            if(!empty($data['error'])){
                return code::error('error',$data['error']);
            }else{
                return code::success();
            }
        }
    }


}
